/*
 * @Description:
 * @version:
 * @Author: 尹鹏孝
 * @Date: 2023-03-16 16:04:47
 * @LastEditors: 尹鹏孝
 * @LastEditTime: 2023-03-16 16:20:58
 */
function compose(middleware){
    return function(context,next){
        let index = -1;
        return dispatch(0)
        function dispatch(){
            if(i<=index){
                return Promise.reject(new Error('next called muill times'))
            }

            index=i;
            let fn = middleware[i];
            if(i===middleware.length){
                fn=next
            }
            if(!fn){
                return Promise.resolve()
            }
            try {
                return Promise.resolve(fn(context,dispatch.bind(null,i+1)))
            } catch (error) {
                return Promise.reject(err)
            }
        }
    }
}